package SGEngine2

import (
	gl "github.com/gl"
)

var (
	RenderCanvas canvas
)

func init() {
	RenderCanvas.RenderList = new(Array)
	RenderCanvas.TransList = new(Array)
}

type canvas struct {
	RenderList *Array
	TransList  *Array
}

func (c *canvas) Add(node *Node) {
	if node.render.Mat.IsTrans {
		c.TransList.Add(node)
	} else {
		c.RenderList.Add(node)

	}
}
func (c *canvas) Render() {
	RootView.ViewScene.Camera.SetCamera()
	gl.MatrixMode(gl.MODELVIEW)
	gl.PushMatrix()
	for i := 0; i < c.RenderList.Count(); i++ {
		n := c.RenderList.Get(i).(*Node)
		gl.LoadMatrixd((*[16]float64)(n.GetWorldMat()))
		n.render.render()

	}

	gl.Enable(gl.ALPHA_TEST)
	gl.AlphaFunc(gl.GREATER, 0)
	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	for i := 0; i < c.TransList.Count(); i++ {
		n := c.TransList.Get(i).(*Node)
		gl.LoadMatrixd((*[16]float64)(n.GetWorldMat()))
		n.render.render()
	}
	gl.Disable(gl.ALPHA_TEST)
	gl.Disable(gl.BLEND)
	gl.PopMatrix()

}

func (c *canvas) Clear() {
	c.RenderList.Clear()
	c.TransList.Clear()
}
